package com.littcore.saas.rest.assistant;

import java.util.List;

import javax.annotation.Resource;

import org.jsondoc.core.annotation.Api;
import org.jsondoc.core.annotation.ApiMethod;
import org.jsondoc.core.annotation.ApiQueryParam;
import org.jsondoc.core.annotation.ApiResponseObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.littcore.dao.page.IPageList;
import com.littcore.dao.ql.PageParam;
import com.littcore.exception.NotLoginException;
import com.littcore.saap.assistant.service.IUserCommentService;
import com.littcore.saap.assistant.vo.UserCommentVo;
import com.littcore.saap.core.web.util.LoginUtils;
import com.littcore.web.util.WebUtils;

@Api(name = "评论接口", description = "用户评论相关功能")
@Controller
public class CommentServiceController {
    @Resource
    private IUserCommentService userCommentService;
    
    @ApiMethod(description = "添加用户评论")
    @ApiResponseObject
    @ResponseBody
    @RequestMapping 
    public UserCommentVo save(@ApiQueryParam(name = "moduleCode") @RequestParam(required=true) String moduleCode,@ApiQueryParam(name = "recordId") @RequestParam(required=true) Integer recordId,@ApiQueryParam(name = "content") @RequestParam(required=true) String content) throws Exception
    {
      return userCommentService.save(LoginUtils.getTenantId(), LoginUtils.getLoginOpId().intValue(), moduleCode, recordId, content);
 
    }
    
    @ApiMethod(description = "删除用户评论")
    @ApiResponseObject
    @ResponseBody
    @RequestMapping 
    public void delete(@ApiQueryParam(name = "id") @RequestParam Integer id) throws Exception
    {
      userCommentService.delete(id);
    }
    
    
    @ApiMethod(description = "获取模块下某记录的评论")
    @ApiResponseObject
    @ResponseBody
    @RequestMapping 
    public List<UserCommentVo> getComments(@ApiQueryParam(name = "moduleCode") @RequestParam String moduleCode,@ApiQueryParam(name = "recordId") @RequestParam Integer recordId,WebRequest request) throws NotLoginException
    { 
   
      
      //package the params
      PageParam pageParam = WebUtils.getPageParam(request);
      pageParam.addCond("tenantId", LoginUtils.getTenantId());
      pageParam.addCond("moduleCode", moduleCode);  
      pageParam.addCond("recordId", recordId);  
      pageParam.addCond("isDeleted", false);  
      pageParam.addSort("createDatetime", "desc");
      //Get page result
      IPageList pageList = userCommentService.findPage(pageParam);   
      
      return pageList.getRsList(); 
    
    }   
  

}
